package com.zhx.socketio.socketio.socketio;


import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.zhx.socketio.socketio.vo.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用于收到消息后转发的
 */
@Slf4j
@Component("eventListenner")
public class EventListenner {


    @Autowired(required = false)
    private SocketIOServer socketIoServer;

    static ArrayList<UUID> listClient = new ArrayList<>();

    private Map<SocketIOClient, LoginVO> clients=new ConcurrentHashMap<SocketIOClient,LoginVO>();

    static final int limitSeconds = 60;

    @OnConnect
    public void onConnect(SocketIOClient client) {
        listClient.add(client.getSessionId());
        log.info("客户端:" + client.getSessionId() + "已连接");
        client.sendEvent("connectsuccess", new Date(), 1);
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("客户端:" + client.getSessionId() + "已离开");
        clients.remove(client);
    }


    @OnEvent(value = "loginOrgganId")
    public void login(SocketIOClient client, AckRequest request, LoginVO loginVO) {
        System.out.println("发来登录消息：" + loginVO.toString());
        clients.put(client,loginVO);
        sendRecive(client);
    }

    public void sendRecive(SocketIOClient client) {   //这里就是向客户端推消息了
        client.sendEvent("success", new Date(), 1);
    }

}
